AC_PREREQ(2.60)
AC_INIT(connman, 0.42)

AM_INIT_AUTOMAKE([foreign subdir-objects])
AM_CONFIG_HEADER(config.h)

m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])

AM_MAINTAINER_MODE

AC_PREFIX_DEFAULT(/usr/local)

PKG_PROG_PKG_CONFIG

COMPILER_FLAGS

AC_SUBST(abs_top_srcdir)
AC_SUBST(abs_top_builddir)

AC_LANG_C

AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_CC_PIE
AC_PROG_INSTALL
AC_PROG_SED
AC_PROG_AWK
AM_PROG_MKDIR_P

m4_define([_LT_AC_TAGCONFIG], [])
m4_ifdef([AC_LIBTOOL_TAGS], [AC_LIBTOOL_TAGS([])])

AC_DISABLE_STATIC
AC_PROG_LIBTOOL
AC_PROG_NM

AC_ARG_ENABLE(optimization, AC_HELP_STRING([--disable-optimization],
			[disable code optimization through compiler]), [
	if (test "${enableval}" = "no"); then
		CFLAGS="$CFLAGS -O0"
	fi
])

GTK_DOC_CHECK

AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug],
			[enable compiling with debugging information]), [
	if (test "${enableval}" = "yes" &&
				test "${ac_cv_prog_cc_g}" = "yes"); then
		CFLAGS="$CFLAGS -g"
	fi
])

AC_ARG_ENABLE(pie, AC_HELP_STRING([--enable-pie],
			[enable position independent executables flag]), [
	if (test "${enableval}" = "yes" &&
				test "${ac_cv_prog_cc_pie}" = "yes"); then
		CFLAGS="$CFLAGS -fPIE"
		LDFLAGS="$LDFLAGS -pie"
	fi
])

AC_ARG_ENABLE(threads,
	AC_HELP_STRING([--enable-threads], [enable threading support]),
			[enable_threads=${enableval}], [enable_threads="no"])

AC_ARG_ENABLE(ethernet,
	AC_HELP_STRING([--enable-ethernet], [enable Ethernet support]),
			[enable_ethernet=${enableval}], [enable_ethernet="no"])
AM_CONDITIONAL(ETHERNET, test "${enable_ethernet}" != "no")
AM_CONDITIONAL(ETHERNET_BUILTIN, test "${enable_ethernet}" = "builtin")

AC_ARG_ENABLE(newwifi,
	AC_HELP_STRING([--enable-newwifi], [enable New WiFi support]),
			[enable_newwifi=${enableval}], [enable_newwifi="no"])
if (test "${enable_newwifi}" != "no"); then
	AC_PATH_PROG(WPASUPPLICANT, [wpa_supplicant], [],
						$PATH:/sbin:/usr/sbin)
fi
AM_CONDITIONAL(NEWWIFI, test "${enable_newwifi}" != "no")
AM_CONDITIONAL(NEWWIFI_BUILTIN, test "${enable_newwifi}" = "builtin")

AC_ARG_WITH(sslroots, AC_HELP_STRING([--with-sslroots=PATH],
	[path to SSL root certificates]), [path_sslroots=${withval}])
if (test -z "${path_sslroots}"); then
	SSL_ROOTS_CFLAGS=""
else
	SSL_ROOTS_CFLAGS="-DSSL_ROOTS=\\\"${path_sslroots}\\\""
fi
AC_SUBST(SSL_ROOTS_CFLAGS)

AC_ARG_ENABLE(bluetooth,
	AC_HELP_STRING([--enable-bluetooth], [enable Bluetooth support]),
			[enable_bluetooth=${enableval}], [enable_bluetooth="no"])
AM_CONDITIONAL(BLUETOOTH, test "${enable_bluetooth}" != "no")
AM_CONDITIONAL(BLUETOOTH_BUILTIN, test "${enable_bluetooth}" = "builtin")

AC_ARG_ENABLE(modemmgr,
	AC_HELP_STRING([--enable-modemmgr], [enable Modem Manager support]),
			[enable_modemmgr=${enableval}], [enable_modemmgr="no"])
if (test "${enable_modemmgr}" != "no"); then
	PROVIDERDB="mobile-broadband-provider-info/serviceproviders.bfd"
	AC_SUBST(PROVIDERDB)
fi
AM_CONDITIONAL(MODEMMGR, test "${enable_modemmgr}" != "no")
AM_CONDITIONAL(MODEMMGR_BUILTIN, test "${enable_modemmgr}" = "builtin")

AC_ARG_WITH(default_pkcs11, AC_HELP_STRING([--with-default-pkcs11=LIB],
        [specify default pkcs11 library]), [path_pkcs11=${withval}])
if (test -n "${path_pkcs11}"); then
   DEFAULT_PKCS11="${path_pkcs11}"
   AC_SUBST(DEFAULT_PKCS11)
fi
AM_CONDITIONAL(DEFAULT_PKCS11, test -n "${path_pkcs11}")

AC_ARG_WITH(dhcpcd, AC_HELP_STRING([--with-dhcpcd=PROGRAM],
	[specify location of dhcpcd binary]), [path_dhcpcd=${withval}])

AC_ARG_ENABLE(dhcpcd,
	AC_HELP_STRING([--enable-dhcpcd], [enable dhcpcd support]),
			[enable_dhcpcd=${enableval}], [enable_dhcpcd="no"])
if (test "${enable_dhcpcd}" != "no"); then
	if (test -z "${path_dhcpcd}"); then
		AC_PATH_PROG(DHCPCD, [dhcpcd], [], $PATH:/sbin:/usr/sbin)
	else
		DHCPCD="${path_dhcpcd}"
		AC_SUBST(DHCPCD)
	fi
fi
AM_CONDITIONAL(DHCPCD, test "${enable_dhcpcd}" != "no")
AM_CONDITIONAL(DHCPCD_BUILTIN, test "${enable_dhcpcd}" = "builtin")

AC_ARG_WITH(resolvconf, AC_HELP_STRING([--with-resolvconf=PROGRAM],
	[specify location of resolvconf binary]), [path_resolvconf=${withval}])

AC_ARG_ENABLE(resolvconf,
	AC_HELP_STRING([--enable-resolvconf], [enable resolvconf support]),
			[enable_resolvconf=${enableval}], [enable_resolvconf="no"])
if (test "${enable_resolvconf}" != "no"); then
	if (test -z "${path_resolvconf}"); then
		AC_PATH_PROG(RESOLVCONF, [resolvconf], [], $PATH:/sbin:/usr/sbin)
	else
		RESOLVCONF="${path_resolvconf}"
		AC_SUBST(RESOLVCONF)
	fi
fi
AM_CONDITIONAL(RESOLVCONF, test "${enable_resolvconf}" != "no")
AM_CONDITIONAL(RESOLVCONF_BUILTIN, test "${enable_resolvconf}" = "builtin")

AC_ARG_WITH(openvpn, AC_HELP_STRING([--with-openvpn=PROGRAM],
        [specify location of openvpn binary]), [path_openvpn=${withval}])

AC_ARG_ENABLE(openvpn,
	AC_HELP_STRING([--enable-openvpn], [enable openvpn support]),
			[enable_openvpn=${enableval}], [enable_openvpn="no"])
if (test "${enable_openvpn}" != "no"); then
	if (test -z "${path_openvpn}"); then
		AC_PATH_PROG(OPENVPN, [openvpn], [], $PATH:/sbin:/usr/sbin)
	else
		OPENVPN="${path_openvpn}"
		AC_SUBST(OPENVPN)
	fi
fi
AM_CONDITIONAL(OPENVPN, test "${enable_openvpn}" != "no")
AM_CONDITIONAL(OPENVPN_BUILTIN, test "${enable_openvpn}" = "builtin")

AC_ARG_WITH(l2tpipsec, AC_HELP_STRING([--with-l2tpipsec=PROGRAM],
        [specify location of L2TP/IPsec VPN binary]),
	[path_l2tpipsec=${withval}])

AC_ARG_ENABLE(l2tpipsec,
	AC_HELP_STRING([--enable-l2tpipsec], [enable L2TP/IPsec support]),
			[enable_l2tpipsec=${enableval}],
			[enable_l2tpipsec="no"])
if (test "${enable_l2tpipsec}" != "no"); then
	if (test -z "${path_l2tpipsec}"); then
		AC_PATH_PROG(L2TPIPSEC, [l2tpipsec_vpn], [],
			$PATH:/sbin:/usr/sbin)
	else
		L2TPIPSEC="${path_l2tpipsec}"
		AC_SUBST(L2TPIPSEC)
	fi
fi
AM_CONDITIONAL(L2TPIPSEC, test "${enable_l2tpipsec}" != "no")
AM_CONDITIONAL(L2TPIPSEC_BUILTIN, test "${enable_l2tpipsec}" = "builtin")

AC_ARG_ENABLE(loopback,
	AC_HELP_STRING([--enable-loopback], [enable loopback support]),
			[enable_loopback=${enableval}], [enable_loopback="no"])
if (test "${enable_loopback}" != "no"); then
	AC_CHECK_HEADERS(sys/inotify.h, dummy=yes,
			AC_MSG_ERROR(inotify header files are required))

	AC_CHECK_LIB(c, inotify_init, dummy=yes,
			AC_MSG_ERROR(inotify library support is required))
fi
AM_CONDITIONAL(LOOPBACK, test "${enable_loopback}" != "no")
AM_CONDITIONAL(LOOPBACK_BUILTIN, test "${enable_loopback}" = "builtin")

AC_ARG_ENABLE(dnsproxy,
	AC_HELP_STRING([--enable-dnsproxy], [enable DNS proxy support]),
			[enable_dnsproxy=${enableval}], [enable_dnsproxy="no"])
AM_CONDITIONAL(DNSPROXY, test "${enable_dnsproxy}" != "no")
AM_CONDITIONAL(DNSPROXY_BUILTIN, test "${enable_dnsproxy}" = "builtin")

AC_ARG_ENABLE(bootstat,
	AC_HELP_STRING([--enable-bootstat],
			[enable Chrome OS boot statistics support]),
			[enable_bootstat=${enableval}],
			[enable_bootstat="no"])
AM_CONDITIONAL(BOOTSTAT, test "${enable_bootstat}" != "no")
AM_CONDITIONAL(BOOTSTAT_BUILTIN, test "${enable_bootstat}" = "builtin")

AC_ARG_ENABLE(crosmetrics,
	AC_HELP_STRING([--enable-crosmetrics],
			[enable Chrome OS metrics support]),
			[enable_crosmetrics=${enableval}],
			[enable_crosmetrics="no"])
AM_CONDITIONAL(CROSMETRICS, test "${enable_crosmetrics}" != "no")
AM_CONDITIONAL(CROSMETRICS_BUILTIN, test "${enable_crosmetrics}" = "builtin")

AC_ARG_ENABLE(resolvfiles,

	AC_HELP_STRING([--enable-resolvfiles], [enable per-interface resolv.conf support]),
			[enable_resolvfiles=${enableval}], [enable_resolvfiles="no"])
AM_CONDITIONAL(RESOLVFILES, test "${enable_resolvfiles}" != "no")
AM_CONDITIONAL(RESOLVFILES_BUILTIN, test "${enable_resolvfiles}" = "builtin")

AC_ARG_WITH(pppd, AC_HELP_STRING([--with-pppd=PROGRAM],
	[specify location of pppd binary]), [path_pppd=${withval}])

AC_ARG_ENABLE(ppp, AC_HELP_STRING([--enable-ppp],
		[enable PPP support]), [enable_ppp=${enableval}])
if (test "${enable_ppp}" != "no"); then
	if (test -z "${path_pppd}"); then
		AC_PATH_PROG(PPPD, [pppd], [], $PATH:/sbin:/usr/sbin)
	else
		PPPD="${path_pppd}"
		AC_SUBST(PPPD)
	fi
	AC_CHECK_HEADERS(pppd/pppd.h, ,
			AC_MSG_ERROR(PPP development files are required))
fi
AM_CONDITIONAL(PPPD, test "${enable_ppp}" != "no")
AM_CONDITIONAL(PPPD_BUILTIN, test "${enable_ppp}" = "builtin")

AC_CHECK_LIB(dl, dlopen, dummy=yes,
			AC_MSG_ERROR(dynamic linking loader is required))

AC_ARG_WITH(iwmxsdk, AC_HELP_STRING([--with-iwmxsdk=PATH],
						[path to Intel WiMAX SDK]),
				[pkgconfig_iwmxsdk=${withval}/lib/pkgconfig])

AC_ARG_ENABLE(iwmx, AC_HELP_STRING([--enable-iwmx],
		[enable Intel WiMAX support]), [enable_iwmx=${enableval}])
if (test "${enable_iwmx}" = "yes"); then
	enable_threads="yes"
	export PKG_CONFIG_PATH="${pkgconfig_iwmxsdk}"
	PKG_CHECK_MODULES(IWMXSDK, libiWmxSdk-0, dummy=yes,
				AC_MSG_ERROR(Intel WiMAX SDK is required))
	PKG_CONFIG_PATH=""
	AC_SUBST(IWMXSDK_CFLAGS)
	AC_SUBST(IWMXSDK_LIBS)
fi
AM_CONDITIONAL(IWMX, test "${enable_iwmx}" = "yes")

PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.16, dummy=yes,
				AC_MSG_ERROR(GLib >= 2.16 is required))
AC_SUBST(GLIB_CFLAGS)
AC_SUBST(GLIB_LIBS)

if (test "${enable_threads}" = "yes"); then
	AC_DEFINE(NEED_THREADS, 1, [Define if threading support is required])
	PKG_CHECK_MODULES(GTHREAD, gthread-2.0 >= 2.16, dummy=yes,
				AC_MSG_ERROR(GThread >= 2.16 is required))
	GLIB_CFLAGS="$GLIB_CFLAGS $GTHREAD_CFLAGS"
	GLIB_LIBS="$GLIB_LIBS $GTHREAD_LIBS"
fi

PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.0, dummy=yes,
				AC_MSG_ERROR(D-Bus >= 1.0 is required))
AC_CHECK_LIB(dbus-1, dbus_watch_get_unix_fd, dummy=yes,
	AC_DEFINE(NEED_DBUS_WATCH_GET_UNIX_FD, 1,
		[Define to 1 if you need the dbus_watch_get_unix_fd() function.]))
AC_SUBST(DBUS_CFLAGS)
AC_SUBST(DBUS_LIBS)

AC_ARG_WITH(dbusconf, AC_HELP_STRING([--with-dbusconf=PATH],
	[path to D-Bus config directory]), [path_dbusconf=${withval}],
		[path_dbusconf="`$PKG_CONFIG --variable=sysconfdir dbus-1`"])
if (test -z "${path_dbusconf}"); then
	DBUS_DATADIR="${sysconfdir}/dbus-1/system.d"
else
	DBUS_DATADIR="${path_dbusconf}/dbus-1/system.d"
fi
AC_SUBST(DBUS_DATADIR)

AC_ARG_ENABLE(nss,
	AC_HELP_STRING([--enable-nss], [enable NSS certificate support]),
			[enable_nss=${enableval}], [enable_nss="no"])
if (test "${enable_nss}" != "no"); then
	PKG_CHECK_MODULES(NSS, nss > 3.12, dummy=yes,
			       AC_MSG_ERROR(NSS >= 3.12 is required))
	AC_SUBST(NSS_CFLAGS)
	AC_SUBST(NSS_LIBS)
	ENABLE_NSS_CFLAGS="-DENABLE_NSS"
	AC_SUBST(ENABLE_NSS_CFLAGS)
fi
AM_CONDITIONAL(NSS, test "${enable_nss}" != "no")
AM_CONDITIONAL(NSS_BUILTIN, test "${enable_nss}" = "builtin")

AC_ARG_ENABLE(polkit,
	AC_HELP_STRING([--enable-polkit], [enable PolicyKit support]),
			[enable_polkit=${enableval}], [enable_polkit="no"])
if (test "${enable_polkit}" != "no"); then
	PKG_CHECK_MODULES(POLKIT, polkit-dbus >= 0.7, dummy=yes,
				AC_MSG_ERROR(PolicyKit >= 0.7 is required))
	AC_SUBST(POLKIT_CFLAGS)
	AC_SUBST(POLKIT_LIBS)
	POLKIT_DATADIR="`$PKG_CONFIG --variable=policydir polkit`"
	if (test -z "${POLKIT_DATADIR}"); then
		POLKIT_DATADIR="${datadir}/PolicyKit/policy"
	fi
	AC_SUBST(POLKIT_DATADIR)
fi
AM_CONDITIONAL(POLKIT, test "${enable_polkit}" != "no")
AM_CONDITIONAL(POLKIT_BUILTIN, test "${enable_polkit}" = "builtin")

AC_ARG_ENABLE(client, AC_HELP_STRING([--enable-client],
		[enable command line client]), [enable_client=${enableval}])
AM_CONDITIONAL(CLIENT, test "${enable_client}" = "yes")

AC_ARG_ENABLE(tools, AC_HELP_STRING([--enable-tools],
		[enable testing tools]), [enable_tools=${enableval}])
if (test "${enable_tools}" = "yes"); then
	PKG_CHECK_MODULES(NETLINK, libnl-1, dummy=yes,
				AC_MSG_ERROR(Netlink library is required))
	AC_SUBST(NETLINK_CFLAGS)
	AC_SUBST(NETLINK_LIBS)
fi
AM_CONDITIONAL(TOOLS, test "${enable_tools}" = "yes")

AC_ARG_ENABLE(fake, AC_HELP_STRING([--enable-fake],
		[enable fake device support]), [enable_fake=${enableval}])
AM_CONDITIONAL(FAKE, test "${enable_fake}" = "yes")

AC_ARG_ENABLE(rot47,
	AC_HELP_STRING([--enable-crypto-rot47], [enable ROT-47 support]),
			[enable_rot47=${enableval}], [enable_rot47="yes"])
AM_CONDITIONAL(ROT47, test "${enable_rot47}" = "yes")
AM_CONDITIONAL(ROT47_BUILTIN, test "${enable_rot47}" = "builtin")

AC_ARG_ENABLE(descbc,
	AC_HELP_STRING([--enable-crypto-descbc], [enable DES-CBC support]),
			[enable_descbc=${enableval}], [enable_descbc="yes"])
AM_CONDITIONAL(DESCBC, test "${enable_descbc}" = "yes")
AM_CONDITIONAL(DESCBC_BUILTIN, test "${enable_descbc}" = "builtin")

AC_ARG_ENABLE(testtools, AC_HELP_STRING([--enable-testtools],
        [enable python testtools support]), [enable_testtools=${enableval}])
AM_CONDITIONAL(TESTTOOLS, test "${enable_testtools}" = "yes")

AC_ARG_ENABLE(udev, AC_HELP_STRING([--disable-udev],
			[don't use udev support even if available]),
						[enable_udev=${enableval}])
if (test "${enable_udev}" != "no"); then
	PKG_CHECK_MODULES(UDEV, libudev >= 141,
				[enable_udev="yes"], [enable_udev="no"])
fi
if (test "${enable_udev}" = "yes"); then
	AC_CHECK_LIB(udev, udev_monitor_filter_update, dummy=yes,
		AC_DEFINE(NEED_UDEV_MONITOR_FILTER, 1,
			[Define to 1 if you need the
					udev_monitor_filter...() functions.]))
	UDEV_DATADIR="`$PKG_CONFIG --variable=udevdir udev`"
	if (test -z "${UDEV_DATADIR}"); then
		UDEV_DATADIR="${sysconfdir}/udev/rules.d"
	else
		UDEV_DATADIR="${UDEV_DATADIR}/rules.d"
	fi
	AC_SUBST(UDEV_DATADIR)
fi
AC_SUBST(UDEV_CFLAGS)
AC_SUBST(UDEV_LIBS)
AM_CONDITIONAL(UDEV, test "${enable_udev}" = "yes")

AC_ARG_ENABLE(datafiles, AC_HELP_STRING([--disable-datafiles],
			[don't install configuration and data files]),
					[enable_datafiles=${enableval}])
AM_CONDITIONAL(DATAFILES, test "${enable_datafiles}" != "no")

AC_ARG_WITH(homedir, AC_HELP_STRING([--with-homedir=PATH],
	[path to user home directory]), [path_homedir=${withval}],
		[path_homedir="/home/%s/user"])
if (test -z "${path_homedir}"); then
	STORAGE_HOMEDIR="/home/%s/user/flimflam"
else
	STORAGE_HOMEDIR="${path_homedir}/flimflam"
fi
AC_SUBST(STORAGE_HOMEDIR)

PKG_CHECK_MODULES(CARES, libcares >= 1.7.0, dummy=yes,
				AC_MSG_ERROR(CARES >= 1.7.0 is required))
AC_SUBST(CARES_CFLAGS)
AC_SUBST(CARES_LIBS)

AC_ARG_ENABLE(hostroute,
	AC_HELP_STRING([--enable-hostroute],
			[enable hostroute support]),
			[enable_hostroute=${enableval}],
			[enable_hostroute="no"])
AM_CONDITIONAL(HOSTROUTE, test "${enable_hostroute}" != "no")
AM_CONDITIONAL(HOSTROUTE_BUILTIN, test "${enable_hostroute}" = "builtin")

AC_OUTPUT(Makefile include/version.h src/flimflam.service
				scripts/flimflam doc/version.xml flimflam.pc)
